//
//  ClassificationCell.swift
//  MyEcho
//
//  Created by longlong on 16/1/25.
//  Copyright © 2016年 LongLong. All rights reserved.
//


class ClassificationCell: UICollectionViewCell,UICollectionViewDataSource, UICollectionViewDelegate{
    var categoryArray:NSMutableArray = [];
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func awakeFromNib() {
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        requstData();
    }
    
    func requstData(){
        let path = NSBundle.mainBundle().pathForResource("JSON_CategoryInfo", ofType: "txt");
        let data:NSData = NSData.dataWithContentsOfMappedFile(path!) as! NSData;
        
        let responseObject = try?NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments);
        let array = responseObject?.valueForKey("result")?.valueForKey("data") as! NSArray
        
        for var obj in array{
            let categoryList = CategoryList();
            categoryList.setValuesForKeysWithDictionary(obj as! [String : AnyObject]);
            self.categoryArray.addObject(categoryList);
        }
        self.collectionView.reloadData();
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.categoryArray.count;
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ClassificationSubCell", forIndexPath: indexPath) as! ClassificationSubCell;
        let categoryList = self.categoryArray[indexPath.row] as! CategoryList;
        cell.imgView.sd_setImageWithURL(NSURL.init(string: categoryList.ico_url as String), placeholderImage: nil)
        cell.nameLabel.text = categoryList.name as String;
        return cell;
    }
}
